package org.apache.directory.mavibot.btree;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.directory.mavibot.btree.BTree;
import org.apache.directory.mavibot.btree.RecordManager;
import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
import org.apache.directory.mavibot.btree.serializer.IntSerializer;
import org.apache.directory.mavibot.btree.serializer.LongSerializer;
import org.xml.sax.SAXException;


public class TestMavibot {
	public static void main (String [] args) throws IOException, ParserConfigurationException, SAXException, KeyNotFoundException
	{
//		List<Tuple<Integer, Integer>> sortedTuple = new ArrayList<Tuple<Integer, Integer>>();
		IntSerializer ser = new IntSerializer();
		BTree<Integer, Integer> btree = null; //BTreeFactory.createPersistedBTree("test", ser, ser);
		BTree<Integer , Integer> btreeDuplicate = null;
        
        RecordManager recordManager = new RecordManager("BtreeSave");

        try {
			btree = recordManager.addBTree("master", ser, ser, false);
			btree.setPageSize(4);
			
		} catch (BTreeAlreadyManagedException e) {
			System.out.println("The Btree is already managed");
			e.printStackTrace();
		}
        
        for ( int i = 1; i <= 11; i++ )
        {
            btree.insert(i, i);
            btree.flush();
            
            
        }
//        System.out.println("Nous sommes à la révision" + btree.getRootPage().getRevision());
//        TupleCursor<Integer,Integer> revisionCursor = btree.browse(11);
//        System.out.println("Le premier revisionCursor a comme révision: " + revisionCursor.getRevision());
        
        if (btree.getRootPage().isNode())
        {
        	PersistedNode<Integer, Integer> rootNode = (PersistedNode<Integer,Integer>) btree.getRootPage();
        	System.out.println("L'offset de la prochaine page est du noeud root est: " +  rootNode.getOffset());
        	System.out.println("Le last offset du noeud root est: " +  rootNode.getLastOffset());
        	
        	
        	// Le nombre de fils est égal au nolbre de clés + 1
        	System.out.println("La clé du premier fils du rootnode est " + rootNode.getKeyHolder(1).getKey());
        	
        	// On recupère la page à la position demandée
        	System.out.println("La valeur contenu dans le noeud a la position 2 est " +  rootNode.getPage(2).getKey(0));
        	
        	
        	// On charge tout le root Node avec les références sur les children
        	PersistedPageHolder<Integer, Integer> firstChild = (PersistedPageHolder<Integer, Integer>) rootNode.children[0];
        	PersistedPageHolder<Integer, Integer> secondChild = (PersistedPageHolder<Integer, Integer>) rootNode.children[1];
        	
        	
        	
        	// test 
        	System.out.println("L'offset du premier enfant est" + firstChild.getOffset());
        	
        	System.out.println("L'offset du second enfant est" + secondChild.getOffset());
        	
            File file = new File("BtreeSave");
            RandomAccessFile raf = new RandomAccessFile(file,"r");
        	byte [] longueur = new byte [4];
        	byte [] firstOffset = new byte[8];
            try {
            	raf.seek(3392);
            	raf.read(longueur, 0, 4);
            	raf.seek(3384);
            	raf.read(firstOffset,0,8);
            }
            finally {
            	raf.close();
            }
            
            IntSerializer ints = new IntSerializer();
            LongSerializer longs = new LongSerializer();
            System.out.println("La longueur est " + ints.fromBytes(longueur));
            System.out.println("L'offset de la prochaine page est " + longs.fromBytes(firstOffset));
        	
        	
        	;
        	
        	// Different test to know if the it is the same btree in the children and the getValue() of the children.  
//        	System.out.println("---------------------------------------------------------------------------------------------");
//        	System.out.println("Le nombre d'élements associé au btree dans le premier fils est " + ((AbstractPage<Integer, Integer>)rootNode.children[0].getValue()).getBtree());
//        	System.out.println("---------------------------------------------------------------------------------------------");
//        	System.out.println("Le nombre d'éléments associé au btree qui est dans le noeud racine est: " + rootNode.children[0].btree);
//        	System.out.println("---------------------------------------------------------------------------------------------");
//        	System.out.println("Comparaison des btree " + (((AbstractPage<Integer, Integer>)rootNode.children[0].getValue()).getBtree() ==  rootNode.children[0].btree));
        }
        else if (btree.getRootPage().isLeaf())
        {
        	PersistedLeaf<Integer, Integer> rootLeaf = (PersistedLeaf<Integer,Integer>) btree.getRootPage();
        	ValueHolder<Integer> [] resultChildren =rootLeaf.values;
        	for (ValueHolder <Integer> vh: resultChildren)
        	{
        		PersistedValueHolder<Integer> pvh = (PersistedValueHolder<Integer>) vh;
        		System.out.println(pvh.valueArray[0]);
        	}
        }
        
        
        // we test the reading of the file to check if the length is the same
        File file = new File("BtreeSave");
        RandomAccessFile raf = new RandomAccessFile(file,"r");
    	byte [] longueur = new byte [4];
        try {
        	raf.seek(6664);
        	raf.read(longueur, 0, 4);
        }
        finally {
        	raf.close();
        }
        
        IntSerializer ints = new IntSerializer();
        System.out.println("La longueur est " + ints.fromBytes(longueur));
        
        System.out.println(btree.toString());
        
        
        JOptionPane.showMessageDialog(null, "On a fini de faire le traitement");
        
        recordManager.close();
	}
}
